<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">耕地保护田长制</h3>
      </div>

      <el-form-item prop="phone">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="phone"
          v-model="loginForm.phone"
          placeholder="用户名"
          name="phone"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        class="log"
        style="width: 100%; margin-bottom: 30px; height: 50px"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >

      <div class="tips">
        <span style="margin-left: 20px; margin-right: 230px">注册</span>
        <span
          style="cursor: pointer; color: #0089fe; color: #333"
          @click="forgetPassword = true"
        >
          忘记密码</span
        >
      </div>
    </el-form>

    <el-dialog class="openLay" title="忘记密码" :visible.sync="forgetPassword">
      <forget-password :phone="loginForm.phone"></forget-password>
    </el-dialog>
  </div>
</template>


<script>
// import { validphone } from "@/utils/validate";
import $ from 'jquery'
import md5 from 'js-md5'
let Base64 = require('js-base64').Base64
import ShowDialog from './components/showDialog'
import ForgetPassword from './components/forgetPassword'
import Registered from './components/registered'

export default {
  name: 'Login',
  components: { ShowDialog, ForgetPassword, Registered },
  data() {
    const validatephone = (rule, value, callback) => {
      if (value.length != 11) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        phone: 'wyl2',
        password: '1',
      },

      forgetPassword: false,

      loginRules: {
        // phone: [
        //   { required: true, trigger: 'blur', validator: validatephone },
        // ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword },
        ],
      },
      loading: false,
      zc: false,

      passwordType: 'password',
      redirect: undefined,
      showDialog: false,
      forgetPassword: false,
      registered: false,
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  methods: {
    updataRegistered(data) {
      this.registered = false
      console.log(data)
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // this.$router.push({ path: this.redirect || '/' })

      let paramt = {
        phone: this.loginForm.phone,
        // password: this.$md5(this.loginForm.password),
        password: Base64.encode(this.loginForm.password),
      }
      let that = this
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', paramt)
            .then((res) => {
              //vuex中的数据刷新页面会消失
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch((err) => {
              this.loading = false
            })
        } else {
          console.log('请填写正确的账号密码')
          return false
        }
      })
    },
  },
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #e5e5e5;
$light_gray: #333;
$cursor: #333;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     color: $cursor;
//   }
// }

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #555;
$light_gray: #444;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background: url(../../assets/login/bgc1.jpg) no-repeat;
  background-size: cover;
  overflow: hidden;
  position: relative;

  .login-form {
    position: absolute;
    width: 420px;
    max-width: 100%;
    padding: 160px 35px 0;
    // margin: 0 auto;
    overflow: hidden;
    right: 200px;
  }

  .tips {
    font-size: 14px;
    color: #333;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    color: #333;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      letter-spacing: 10px;
    }
  }

  .log {
    background-color: #06ac6a;
    border: #06ac6a;
    color: #fff;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>